{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在进行热毒蕴结证型系数的聚类\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda2\\lib\\site-packages\\ipykernel_launcher.py:42: FutureWarning: pd.rolling_mean is deprecated for Series and will be removed in a future version, replace with \n",
      "\tSeries.rolling(window=2,center=False).mean()\n",
      "D:\\Anaconda2\\lib\\site-packages\\ipykernel_launcher.py:43: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在进行冲任失调证型系数的聚类\n",
      "正在进行肝肾阴虚证型系数的聚类\n",
      "正在进行气血两虚证型系数的聚类\n",
      "正在进行肝气郁结证型系数的聚类\n",
      "正在进行脾胃虚弱证型系数的聚类\n",
      "        1           2           3           4\n",
      "A     0.0    0.178698    0.257724    0.351843\n",
      "An  240.0  356.000000  281.000000   53.000000\n",
      "B     0.0    0.147923    0.287039    0.459367\n",
      "Bn  316.0  394.000000  174.000000   46.000000\n",
      "C     0.0    0.202149    0.289061    0.423537\n",
      "Cn  297.0  394.000000  204.000000   35.000000\n",
      "D     0.0    0.176448    0.256805    0.365095\n",
      "Dn  309.0  370.000000  211.000000   40.000000\n",
      "E     0.0    0.152698    0.257873    0.376062\n",
      "En  273.0  319.000000  245.000000   93.000000\n",
      "F     0.0    0.179143    0.261386    0.354643\n",
      "Fn  200.0  237.000000  265.000000  228.000000\n"
     ]
    }
   ],
   "source": [
    "# 1>  数据预处理 \n",
    "\n",
    "# 1数据清洗\n",
    "# 2属性规约\n",
    "# 3数据变换\n",
    "# （1）属性构造\n",
    "# （2）数据离散化\n",
    "\n",
    "# -*- coding:utf-8 -*-\n",
    "from __future__ import print_function\n",
    "import pandas as pd\n",
    "from pandas import DataFrame,Series\n",
    "from sklearn.cluster import KMeans#导入K均值聚类算法\n",
    "\n",
    "datafile = 'data.xls'\n",
    "resultfile = 'data_processed.xlsx'\n",
    "\n",
    "typelabel = {u'肝气郁结证型系数':'A',u'热毒蕴结证型系数':'B',u'冲任失调证型系数':'C',u'气血两虚证型系数':'D',u'脾胃虚弱证型系数':'E',u'肝肾阴虚证型系数':'F'}\n",
    "\n",
    "k = 4 #需要进行的聚类类别数\n",
    "\n",
    "#读取文件进行聚类分析\n",
    "data = pd.read_excel(datafile)\n",
    "keys = list(typelabel.keys())\n",
    "result = DataFrame()\n",
    "\n",
    "for i in range(len(keys)):\n",
    "    #调用k-means算法 进行聚类\n",
    "    print(u'正在进行%s的聚类' % keys[i])\n",
    "    kmodel = KMeans(n_clusters = k, n_jobs = 4)  # n_job是线程数，根据自己电脑本身来调节\n",
    "    kmodel.fit(data[[keys[i]]].as_matrix())# 训练模型\n",
    "#     kmodel.fit(data[[keys[i]]]) # 不转成矩阵形式结果一样\n",
    "#KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,\n",
    "#     n_clusters=4, n_init=10, n_jobs=4, precompute_distances='auto',\n",
    "#     random_state=None, tol=0.0001, verbose=0)\n",
    "    \n",
    "    r1 = DataFrame(kmodel.cluster_centers_, columns = [typelabel[keys[i]]]) # 聚类中心\n",
    "    r2 = Series(kmodel.labels_).value_counts() #分类统计\n",
    "    r2 = DataFrame(r2,columns = [typelabel[keys[i]]+'n'])# 转成DataFrame格式，记录各个类别的数目\n",
    "    r = pd.concat([r1,r2], axis=1).sort_values(typelabel[keys[i]])\n",
    "    r.index = range(1,5)\n",
    "    r[typelabel[keys[i]]] = pd.rolling_mean(r[typelabel[keys[i]]],2) # rolling_mean用来计算相邻两列的均值，以此作为边界点\n",
    "    r[typelabel[keys[i]]][1] = 0.0 # 将原来的聚类中心改成边界点\n",
    "    result = result.append(r.T)\n",
    "result = result.sort_index() # 以index排序，以ABCDEF排序\n",
    "result.to_excel(resultfile)\n",
    "    \n",
    "print (result)\n",
    "# '''\n",
    "#         1           2           3           4\n",
    "# A     0.0    0.178698    0.257724    0.351843\n",
    "# An  240.0  356.000000  281.000000   53.000000\n",
    "# B     0.0    0.150766    0.296631    0.489705\n",
    "# Bn  325.0  396.000000  180.000000   29.000000\n",
    "# C     0.0    0.202149    0.289061    0.423537\n",
    "# Cn  297.0  394.000000  204.000000   35.000000\n",
    "# D     0.0    0.172049    0.251583    0.359353\n",
    "# Dn  283.0  375.000000  228.000000   44.000000\n",
    "# E     0.0    0.152698    0.257762    0.375661\n",
    "# En  273.0  319.000000  244.000000   94.000000\n",
    "# F     0.0    0.179143    0.261386    0.354643\n",
    "# Fn  200.0  237.000000  265.000000  228.000000\n",
    "# '''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>肝气郁结证型系数</th>\n",
       "      <th>热毒蕴结证型系数</th>\n",
       "      <th>冲任失调证型系数</th>\n",
       "      <th>气血两虚证型系数</th>\n",
       "      <th>脾胃虚弱证型系数</th>\n",
       "      <th>肝肾阴虚证型系数</th>\n",
       "      <th>病程阶段</th>\n",
       "      <th>TNM分期</th>\n",
       "      <th>转移部位</th>\n",
       "      <th>确诊后几年发现转移</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.056</td>\n",
       "      <td>0.460</td>\n",
       "      <td>0.281</td>\n",
       "      <td>0.352</td>\n",
       "      <td>0.119</td>\n",
       "      <td>0.350</td>\n",
       "      <td>S4</td>\n",
       "      <td>H4</td>\n",
       "      <td>R1</td>\n",
       "      <td>J1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.488</td>\n",
       "      <td>0.099</td>\n",
       "      <td>0.283</td>\n",
       "      <td>0.333</td>\n",
       "      <td>0.116</td>\n",
       "      <td>0.293</td>\n",
       "      <td>S4</td>\n",
       "      <td>H4</td>\n",
       "      <td>R1</td>\n",
       "      <td>J1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.107</td>\n",
       "      <td>0.008</td>\n",
       "      <td>0.204</td>\n",
       "      <td>0.150</td>\n",
       "      <td>0.032</td>\n",
       "      <td>0.159</td>\n",
       "      <td>S4</td>\n",
       "      <td>H4</td>\n",
       "      <td>R2</td>\n",
       "      <td>J2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.322</td>\n",
       "      <td>0.208</td>\n",
       "      <td>0.305</td>\n",
       "      <td>0.130</td>\n",
       "      <td>0.184</td>\n",
       "      <td>0.317</td>\n",
       "      <td>S4</td>\n",
       "      <td>H4</td>\n",
       "      <td>R2</td>\n",
       "      <td>J1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.242</td>\n",
       "      <td>0.280</td>\n",
       "      <td>0.131</td>\n",
       "      <td>0.210</td>\n",
       "      <td>0.191</td>\n",
       "      <td>0.351</td>\n",
       "      <td>S4</td>\n",
       "      <td>H4</td>\n",
       "      <td>R2R5</td>\n",
       "      <td>J1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   肝气郁结证型系数  热毒蕴结证型系数  冲任失调证型系数  气血两虚证型系数  脾胃虚弱证型系数  肝肾阴虚证型系数 病程阶段 TNM分期  \\\n",
       "0     0.056     0.460     0.281     0.352     0.119     0.350   S4    H4   \n",
       "1     0.488     0.099     0.283     0.333     0.116     0.293   S4    H4   \n",
       "2     0.107     0.008     0.204     0.150     0.032     0.159   S4    H4   \n",
       "3     0.322     0.208     0.305     0.130     0.184     0.317   S4    H4   \n",
       "4     0.242     0.280     0.131     0.210     0.191     0.351   S4    H4   \n",
       "\n",
       "   转移部位 确诊后几年发现转移  \n",
       "0    R1        J1  \n",
       "1    R1        J1  \n",
       "2    R2        J2  \n",
       "3    R2        J1  \n",
       "4  R2R5        J1  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2>划分原始数据中的类别\n",
    "import pandas as pd\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>肝气郁结证型系数</th>\n",
       "      <th>热毒蕴结证型系数</th>\n",
       "      <th>冲任失调证型系数</th>\n",
       "      <th>气血两虚证型系数</th>\n",
       "      <th>脾胃虚弱证型系数</th>\n",
       "      <th>肝肾阴虚证型系数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A1</td>\n",
       "      <td>B4</td>\n",
       "      <td>C2</td>\n",
       "      <td>D3</td>\n",
       "      <td>E1</td>\n",
       "      <td>F3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A4</td>\n",
       "      <td>B1</td>\n",
       "      <td>C2</td>\n",
       "      <td>D3</td>\n",
       "      <td>E1</td>\n",
       "      <td>F3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>C2</td>\n",
       "      <td>D1</td>\n",
       "      <td>E1</td>\n",
       "      <td>F1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3</td>\n",
       "      <td>B2</td>\n",
       "      <td>C3</td>\n",
       "      <td>D1</td>\n",
       "      <td>E2</td>\n",
       "      <td>F3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C1</td>\n",
       "      <td>D2</td>\n",
       "      <td>E2</td>\n",
       "      <td>F3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  肝气郁结证型系数 热毒蕴结证型系数 冲任失调证型系数 气血两虚证型系数 脾胃虚弱证型系数 肝肾阴虚证型系数\n",
       "0       A1       B4       C2       D3       E1       F3\n",
       "1       A4       B1       C2       D3       E1       F3\n",
       "2       A1       B1       C2       D1       E1       F1\n",
       "3       A3       B2       C3       D1       E2       F3\n",
       "4       A2       B2       C1       D2       E2       F3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将分类后数据进行处理（*****）\n",
    "data_cut = DataFrame(columns = data.columns[:6])\n",
    "types = ['A','B','C','D','E','F']\n",
    "num = ['1','2','3','4']\n",
    "for i in range(len(data_cut.columns)):\n",
    "    value = list(data.iloc[:,i])\n",
    "    bins = list(result[(2*i):(2*i+1)].values[0])\n",
    "    bins.append(1)\n",
    "    names = [str(x)+str(y) for x in types for y in num]\n",
    "    group_names = names[4*i:4*(i+1)]\n",
    "    cats = pd.cut(value,bins,labels=group_names,right=False)\n",
    "    data_cut.iloc[:,i] = cats\n",
    "data_cut.to_excel('apriori.xlsx')\n",
    "data_cut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
